package ruifeng.zcj.communcationring.utils;

import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Environment;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;

import ruifeng.zcj.communcationring.groble.utils.Validate;

public class Media {
    // 录音文件播放
    private MediaPlayer myPlayer;
    // 录音
    private MediaRecorder myRecorder;
    // 音频文件保存地址
    public String sendpath, receivepath;// path/xx.amr就是完整路径
    public String name;// 存储名字
    private File saveFilePath;

    public Media() {
        if (Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {
            try {
                sendpath = Environment.getExternalStorageDirectory()
                        .getCanonicalPath().toString()
                        + "/zcj";
                File files = new File(sendpath);
                if (!files.exists()) {
                    files.mkdir();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    // 开始录制
    public void startRecord() {
        if (!Environment.getExternalStorageState().equals(
                android.os.Environment.MEDIA_MOUNTED)) {
            return;
        }
        if (myRecorder != null) {
            myRecorder.release();
            myRecorder = null;
        }
        myRecorder = new MediaRecorder();
        // 从麦克风源进行录音
        myRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        // 设置输出格式   AMR_NB
        myRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
        // 设置编码格式   AMR_NB
        myRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);


        myRecorder.setAudioChannels(1); // MONO
        myRecorder.setAudioSamplingRate(8000); // 8000Hz
        myRecorder.setAudioEncodingBitRate(64);

        // 保存路径
        this.name = "AND"
                + new SimpleDateFormat("yyyyMMddHHmmss").format(System
                .currentTimeMillis()) + ".aac";
        String paths = sendpath + "/" + name;
        saveFilePath = new File(paths);
        myRecorder.setOutputFile(saveFilePath.getAbsolutePath());
        try {
            saveFilePath.createNewFile();
            myRecorder.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 开始录音
        myRecorder.start();
    }

    // 停止录制并保存
    public void stopRecord() {
        if (saveFilePath.exists() && saveFilePath != null) {
            myRecorder.stop();
            myRecorder.release();
        }
    }

    // 退出
    public void destroy() {
        // 释放资源
        if (myPlayer != null && myRecorder != null) {
            if (myPlayer.isPlaying()) {
                myPlayer.stop();
                myPlayer.release();
            }
            myPlayer.release();
            myRecorder.release();
        }
    }

    // 开始播放
    public void startPlay(String path0) {
        myPlayer = new MediaPlayer();
        try {
            myPlayer.reset();
            myPlayer.setDataSource(path0);
            if (!myPlayer.isPlaying()) {

                myPlayer.prepare();
                myPlayer.start();
            } else {
                myPlayer.pause();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 停止播放
    public void stopPlay() {
        if (myPlayer.isPlaying()) {
            myPlayer.stop();
        }
    }

    /**
     * 删除这个文件夹下面的所有文件
     */
    public void deleteMusic() {
        File pathMusicPath = new File(sendpath);
        deleteAllFiles(pathMusicPath);
    }

    private void deleteAllFiles(File root) {
        File files[] = root.listFiles();
        if (files != null)
            for (File f : files) {
                if (f.isDirectory()) { // 判断是否为文件夹
                    deleteAllFiles(f);
                    try {
                        f.delete();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else {
                    if (f.exists()) { // 判断是否存在
                        deleteAllFiles(f);
                        try {
                            f.delete();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
    }

}
